﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MF.Networker;
using System.Net.Sockets;
using Game.Foundation;

namespace Game.Server.ChineseChess
{
    class ClientManager : IDisposable
    {
        public static readonly int ActiveSeconds = 45;

        private AsyncWorkSocket _netWorker;

        public ClientStatus Status { get; set; }

        public GameProxy GameProxy { get; set; }

        public string UserName { get; private set; }

        public DateTime OverdueTime { get; private set; }

        public event EventHandler<LoginEventArgs> ReceivedLoginRequest;
        public event EventHandler<JoinGameGroupEventArgs> ReceivedJoinGameGroupRequest;
        public event EventHandler<ReadyOperationEventArgs> ReceivedReadyOperationRequest;
        public event EventHandler<GameOperationEventArgs> ReceivedGameOperationRequest;
        public event EventHandler<ChatEventArgs> ReceivedChatMessage;

        public ClientManager(Socket client)
        {
            this.Status = ClientStatus.Initial;
            this.OverdueTime = DateTime.Now.AddSeconds(ActiveSeconds);
            this._netWorker = new MF.Networker.AsyncWorkSocket(client, GamePacketLoader.Default);
            this._netWorker.PacketReceived += new EventHandler<PacketEventArgs>(_netWorker_PacketReceived);
        }

        public void SendMessage(IPacket packet)
        {
            this._netWorker.SendAsync(packet);
        }

        public void SendMessage(IEnumerable<IPacket> packetList)
        {
            this._netWorker.SendAsync(packetList);
        }

        void _netWorker_PacketReceived(object sender, PacketEventArgs e)
        {
            GamePacket p = e.Packet as GamePacket;

            switch (p.Type)
            {
                case PacketType.Pong:
                    this.OverdueTime = DateTime.Now.AddSeconds(ActiveSeconds);
                    break;

                case PacketType.进入房间请求:
                    this.ProcessLoginRequest(p);
                    break;

                case PacketType.发送聊天信息请求:
                    this.ProcessChatRequest(p);
                    break;

                case PacketType.加入游戏分组请求:
                    ProcessJoinGroupRequest(p);
                    break;

                case PacketType.准备游戏操作请求:
                    ProcessReadyOperation(p);
                    break;

                case PacketType.游戏操作请求包:
                    ProcessGameOperationRequest(p);
                    break;
            }
        }

        //private void ProcessLoginRequest(GamePacket p)
        //{
        //    if (this.ReceivedLoginRequest == null) return;

        //    LoginEventArgs e = new LoginEventArgs();
        //    e.AreaID = p.ReadInt32();
        //    e.RoomID = p.ReadInt32();
        //    e.UserName = p.ReadString();
        //    this.UserName = e.UserName;

        //    this.ReceivedLoginRequest(this, e);
        //}

        //private void ProcessJoinGroupRequest(GamePacket p)
        //{
        //    if (this.ReceivedJoinGameGroupRequest == null) return;

        //    JoinGameGroupEventArgs e = new JoinGameGroupEventArgs();
        //    e.AreaID = p.ReadInt32();
        //    e.RoomID = p.ReadInt32();
        //    e.GroupID = p.ReadInt32();
        //    e.Index = p.ReadByte();

        //    this.ReceivedJoinGameGroupRequest(this, e);
        //}

        //private void ProcessReadyOperation(GamePacket p)
        //{
        //    if (this.ReceivedReadyOperationRequest == null) return;

        //    ReadyOperationEventArgs e = new ReadyOperationEventArgs();
        //    e.Operation = (GameStatus)p.OptionOne;
        //    this.ReceivedReadyOperationRequest(this, e);
        //}

        //private void ProcessGameOperationRequest(GamePacket p)
        //{
        //    if (this.ReceivedGameOperationRequest == null) return;

        //    GameOperationEventArgs e = new GameOperationEventArgs();
        //    e.XOld = p.ReadInt32();
        //    e.YOld = p.ReadInt32();
        //    e.XNew = p.ReadInt32();
        //    e.YNew = p.ReadInt32();

        //    this.ReceivedGameOperationRequest(this, e);
        //}

        //private void ProcessChatRequest(GamePacket p)
        //{
        //    if (this.Status != ClientStatus.Work || this.ReceivedChatMessage == null) return;

        //    ChatEventArgs e = new ChatEventArgs();
        //    string content = p.ReadString();
        //    content = string.Format("[{0}]：{1}", this.UserName, content);
        //    e.Content = content;

        //    this._netWorker.SendAsync(new GamePacket(PacketType.发送聊天信息返回));
        //    this.ReceivedChatMessage(this, e);
        //}

        #region IDisposable 成员

        public void Dispose()
        {
            this._netWorker.Close();
            Console.WriteLine(string.Format("玩家[{0}]的连接已关闭。", this.UserName));
        }

        #endregion
    }
}
